<template>
  <el-dialog
    v-model="state.dialogVisible"
    :append-to-body="true"
    :before-close="close"
    :close-on-click-modal="true"
    :close-on-press-escape="true"
    :destroy-on-close="true"
    :modal="model"
    top="5vh"
    :title="state.dialogTitle"
    :width="width"
  >
    <vab-query-form>
      <vab-query-form-top-panel>
        <el-form
          :inline="true"
          label-width="80px"
          :model="tableOptions.queryForm"
        >
          <el-form-item label="资源地址">
            <el-input
              v-model="tableOptions.queryForm.resourceUrl"
              placeholder="资源地址"
            />
          </el-form-item>
          <el-form-item label="资源名称">
            <el-input
              v-model="tableOptions.queryForm.resourceName"
              placeholder="资源名称"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              icon="refresh-left"
              type="warning"
              @click="handleResetQuery"
            >
              重置
            </el-button>
            <el-button icon="Search" type="primary" @click="queryTableData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-top-panel>
    </vab-query-form>
    <el-table
      ref="tableRef"
      v-loading="tableOptions.loading"
      border
      stripe
      :data="tableOptions.dataList"
      :height="tableOptions.height"
      :cell-style="tableStyle.cellStyle"
      :header-cell-style="tableStyle.headerCellStyle"
      @sort-change="handleSortChange"
      @selection-change="handleSelectionChange"
      @row-click="handleRowClick"
    >
      <el-table-column
        align="center"
        show-overflow-tooltip
        type="selection"
        width="50"
      />
      <el-table-column label="序号" type="index" width="55">
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="资源名称"
        prop="resourceName"
        width="180"
      />
      <el-table-column
        align="center"
        label="资源地址"
        prop="resourceUrl"
        width="340"
      />
      <el-table-column
        align="center"
        label="关联菜单"
        prop="menuFullName"
        width="340"
      />
    </el-table>
    <el-pagination
      background
      :current-page="tableOptions.pagination.current"
      :layout="tableOptions.pagination.layout"
      :page-size="tableOptions.pagination.size"
      :total="tableOptions.pagination.total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <template #footer>
      <el-button icon="circle-close" type="warning" @click="close">
        取消
      </el-button>
      <el-button icon="circle-check" type="primary" @click="save">
        确定
      </el-button>
    </template>
  </el-dialog>
</template>
<script setup name="AvaliableResource">
import { useMessage, useMessageBox } from "@/hooks/message";
import {
  queryRoleAvaliableResourceTable,
  insertRoleResource
} from "@/api/admin/org/role/";
import { useTable } from "@/hooks/table";

const props = defineProps({
  title: {
    type: String,
    default: "可添加资源"
  },
  width: {
    type: String,
    default: "60%"
  },
  model: {
    type: Boolean,
    default: true
  }
});
const emits = defineEmits(["confirm"]);

const tableRef = ref(null);

const tableOptions = reactive({
  autoLoad: false,
  queryForm: {
    roleId: "",
    resourceUrl: "",
    resourceName: ""
  },
  queryFunction: queryRoleAvaliableResourceTable,
  descs: ["create_time"]
});

const {
  tableStyle,
  queryTableData,
  handleSelectionChange,
  handleReset,
  handleResetQuery,
  handleRowClick,
  handleSizeChange,
  handleCurrentChange,
  handleSortChange,
  handleToggleHeight
} = useTable(tableOptions, tableRef);

const state = reactive({
  dialogTitle: props.title,
  dialogVisible: false
});

const save = () => {
  if (tableOptions.dataSelections.length > 0) {
    const ids = tableOptions.dataSelections.map(item => item.id).join();
    useMessageBox()
      .confirm(
        `是否确认添加选择的${tableOptions.dataSelections.length}条资源权限`
      )
      .then(async () => {
        const { code, msg } = await insertRoleResource({
          roleId: tableOptions.queryForm.roleId,
          resourceIds: ids
        });
        if (code === 200) {
          useMessage().success(msg);
          emits("confirm", {});
          close();
        } else {
          useMessage().warning(msg);
        }
      });
  } else {
    useMessage().warning("未选中任何行");
  }
};

const show = roleId => {
  if (roleId) {
    handleToggleHeight(80);
    queryTableData({ roleId: roleId }, true);
    state.dialogVisible = true;
  }
};

const close = () => {
  handleReset();
  state.dialogVisible = false;
};

defineExpose({
  show,
  close,
  save
});
</script>
<style lang="scss" scoped></style>
